const { Model, DataTypes } = require("sequelize");
const sequelize = require("../app/database");
const Question = require("./question.model");

class Result extends Model {}

Result.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    answer_id: {
      type: DataTypes.INTEGER,
      references: {
        model: Question,
        key: "id",
      },
    },
    result_round: {
      type: DataTypes.INTEGER,
    },
    max_average: {
      type: DataTypes.DOUBLE,
    },
    possible_average: {
      type: DataTypes.DOUBLE,
    },
    min_average: {
      type: DataTypes.DOUBLE,
    },
    estimate: {
      type: DataTypes.DOUBLE,
    },
    count: {
      type: DataTypes.INTEGER,
    },
    createAt: {
      type: DataTypes.DATE,
    },
    updateAt: {
      type: DataTypes.DATE,
    },
  },
  {
    tableName: "result",
    createdAt: "createAt",
    updatedAt: "updateAt",
    timestamps: true,
    sequelize,
  }
);

// 一对多
Result.belongsTo(Question, {
  foreignKey: "answer_id",
});

module.exports = Result;
